/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */
#if defined(CONFIG_CLI) && ( defined(CONFIG_TCPIP) || defined(CONFIG_SAL))
#include <aos/cli.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

/* format "%Y-%m-%d %H:%M:%S" */
static char *strptime(const char *s, const char *format, struct tm *tm)
{
    /* time format: 2020-04-25 12:00:00 */
    int y = 0, m = 0, d = 0, hour = 0, min = 0, sec = 0;
    char str_time[64];
    char *cstart = NULL;
    char *date[2] = {NULL, NULL};

    if( s == NULL || tm == NULL) {
        return NULL;
    }

    strncpy(str_time, s, sizeof(str_time) - 1);
    str_time[sizeof(str_time) - 1] = '\0';

    char *saveptr  = NULL;
    date[0] = strtok_r(str_time, " ", &saveptr);
    date[1] = strtok_r(NULL, " ", &saveptr);
    if(NULL == date[0] || NULL == date[1]) {
        return NULL;
    }

    saveptr = NULL;
    cstart = strtok_r(date[0], "-", &saveptr);
    if(cstart) {
        y = atoi(cstart);
        cstart = strtok_r(NULL, "-", &saveptr);
    }
    if(cstart) {
        m = atoi(cstart);
        cstart = strtok_r(NULL, "-", &saveptr);
    }
    if(cstart) {
        d = atoi(cstart);
    }

    saveptr  = NULL;
    cstart = strtok_r(date[1], ":", &saveptr);
    if(cstart) {
        hour = atoi(cstart);
        cstart = strtok_r(NULL, ":", &saveptr);
    }
    if(cstart) {
        min = atoi(cstart);
        cstart = strtok_r(NULL, ":", &saveptr);
    }
    if(cstart) {
        sec = atoi(cstart);
    }

    memset(tm, 0, sizeof(struct tm));
    tm->tm_year = y - 1900;
    tm->tm_mon = m - 1;
    tm->tm_mday = d;
    tm->tm_hour = hour;
    tm->tm_min = min;
    tm->tm_sec = sec;

    return (char *)s;
}

static void cmd_ntp_func(char *wbuf, int wbuf_len, int argc, char **argv)
{
    int item_count = argc;

    if (item_count >= 2) {
        if (strcmp("-s", argv[1]) == 0) {
            struct tm stm;
            if (strptime(argv[2], NULL, &stm) != NULL) {
                time_t ntime = mktime(&stm);

                struct timeval tv;
                tv.tv_sec = ntime;
                tv.tv_usec = 0;
                settimeofday(&tv, NULL);

                printf("set date to: %04d-%02d-%02d %02d:%02d:%02d\n",
                        stm.tm_year + 1900, stm.tm_mon + 1, stm.tm_mday,
                        stm.tm_hour, stm.tm_min, stm.tm_sec);
            }
        }
    }
    
    {
        time_t t = time(NULL);
        time_t lct = t + TIME_ZONE * 3600;
        if (t >= 0) {
            printf("\tTZ(%02d):%s %d\n", TIME_ZONE, ctime(&lct), lct );
            printf("\t   UTC:%s %d\n", ctime(&t), t );
            printf("\t   date -s \"2001-01-01 12:13:14\"\n");
        }
    }
}

void cli_reg_cmd_date(void)
{
    static const struct cli_command cmd_info = {
        "date",
        "date command.",
        cmd_ntp_func
    };

    aos_cli_register_command(&cmd_info);
}

#endif /*defined(CONFIG_CLI) && ( defined(CONFIG_TCPIP)) */
